#include "showret.h"
#include "ui_showret.h"
#include <QStandardItemModel>
#include <qdebug.h>

enum {
    SEND_IDLE,
    SEND_DEAL,
    SEND_SUCCESS,
    SEND_FAIL
};

showret::showret(QString language,QHash<QString,DEV> dev,QWidget *parent) :
    QDialog(parent),
    ui(new Ui::showret)
{
    ui->setupUi(this);

    QHash<QString,DEV>::iterator i;
    int j  = 0;

    QStandardItemModel* model = new QStandardItemModel();
    model->setColumnCount(2);
    if (language == "English") {
        model->setHeaderData(0,Qt::Horizontal,QString("    Device No    "));
        model->setHeaderData(1,Qt::Horizontal,QString("    Status       "));
    }
    if (language == "Chinese") {
        model->setHeaderData(0,Qt::Horizontal,QString("    设备号    "));
        model->setHeaderData(1,Qt::Horizontal,QString("    状  态    "));
    }


    QStandardItem* item = 0;

     //qDebug() << "遍历哈希表如下-------------" << endl;
     for(i = dev.begin(); i != dev.end(); ++i,j++) {
         qDebug() << i.key() <<"        " << i.value().ID << i.value().status;

         item = new QStandardItem(QString("%1").arg(i.value().ID));
         item->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
         model->setItem(j,0,item);
         if (i.value().status == SEND_SUCCESS) {
            if (language == "English")
                 item = new QStandardItem(QString("%1").arg("  Sent successfully  "));
            if (language == "Chinese")
                 item = new QStandardItem(QString("%1").arg("  发送成功  "));

         }
         if (i.value().status == SEND_FAIL) {
             if (language == "English")
                 item = new QStandardItem(QString("%1").arg("  Failed to send  "));
             if (language == "Chinese")
                 item = new QStandardItem(QString("%1").arg("  发送失败  "));

         }

         item->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
         model->setItem(j,1,item);
         //ui->tableView->item(j,0)->setTextAlignment(Qt::AlignHCenter|Qt::AlignVCenter);
         //ui->tableView->item(j,1)->setTextAlignment(Qt::AlignHCenter|Qt::AlignVCenter);
     }


     ui->tableView->setModel(model);
     ui->tableView->show();
    // ui->tableView->horizontalHeader()->setSectionResizeMode(0,QHeaderView::ResizeToContents);
    // ui->tableView->horizontalHeader()->setSectionResizeMode(1,QHeaderView::ResizeToContents);
     ui->tableView->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
     ui->tableView->setEditTriggers(QAbstractItemView::NoEditTriggers);
}

showret::~showret()
{
    delete ui;
}
